package org.jash.demo2205

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hyphenate.chat.ChatClient
import com.hyphenate.helpdesk.callback.Callback
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import org.jash.demo2205.entity.User
import org.jash.demo2205.mvvm.BaseViewModel
import javax.inject.Inject

class LoginViewModel:BaseViewModel() {
    val loginLiveData:MutableLiveData<User> by lazy { MutableLiveData() }
    fun login(user:Map<String, String>) {
        viewModelScope.launch {
            try {
                val res = service.login(user)



                ChatClient.getInstance().login(user["username"]?:"", user["password"]?:"", object : Callback {
                    override fun onSuccess() {
                        Log.d("TAG", "onSuccess: ")
                    }

                    override fun onError(code: Int, error: String?) {
                        Log.d("TAG", "onError: $error")
                    }

                    override fun onProgress(progress: Int, status: String?) {
                    }

                })
                if (res.code ==  200) {
                    loginLiveData.postValue(res.data)
                } else {
                    throw RuntimeException(res.message)
                }
            } catch (e:Exception) {
                failureLiveData.postValue(e)
            }
        }

    }
}